/**
    @author: ChenLin
    @date: 2022/11/10 todo 视频服务处理启动类
**/
package main

import (
	controller "./api"
	handler "./handler"
	"errors"
	"flag"
	"flamingo/base"
	"flamingo/logger"
	"fmt"
	"github.com/Unknwon/goconfig"
	"os"
	"strconv"
	"strings"
)

var (
	mysqlInfo     base.MysqlInfo
	dirs          []string
	receiveUrls   []string
	reportUrls    []string
	key           string
	addr          string
	serverType    string
	stage         int
	idc           string
	optIdcs       []string
	appId         string
	appSecret     string
	hostUpload    string
	hostUpdate    string
	hostCheck     string
	hostCheckUrl  string
	httpPort      string
	promethusPort int
)

type server struct {
	httpVideoServer  *controller.HttpVideoServer
	thriftVideServer *controller.ThriftVideoServer
}

func NewServer() *server {
	ch := make(chan handler.VideoProcess, 1024)
	var server = &server{}
	server.httpVideoServer = controller.NewHttpVideoServer(ch, httpPort)
	server.thriftVideServer = controller.NewThriftVideoServer(ch)
	return server
}

func initialize() error {
	// 是否已经解析
	flag.Parse()
	defer logger.Flush()

	if len(os.Args) < 1 {
		logger.Logln(logger.ERROR, "No conf file")
		return errors.New("No conf file")
	} else {
		return loadcfg(os.Args[len(os.Args)-1])
	}
}

func loadcfg(cfile string) error {
	defer logger.Flush()
	var err error
	cfg, err := goconfig.LoadConfigFile(cfile)
	if err != nil {
		logger.Logln(logger.ERROR, "load config failed , errmsg:", err)
		return err
	}
	addr = cfg.MustValue("ThriftInfo", "addr")
	key = cfg.MustValue("Other", "ETCDKey")
	serverType = cfg.MustValue("Other", "serverType")
	idc = cfg.MustValue("Other", "idc")
	optIdsCStr := cfg.MustValue("Other", "optidcs")
	httpPort = cfg.MustValue("Other", "httpPort")
	stage = cfg.MustInt("Other", "stage")
	if len(optIdsCStr) > 0 {
		ss := strings.Split(optIdsCStr, ",")
		for _, s := range ss {
			if len(s) > 0 {
				optIdcs = append(optIdcs, s)
			}
		}
	}
	keyList := cfg.GetKeyList("Dirs")
	for _, value := range keyList {
		data := cfg.MustValue("Dirs", value)
		dirs = append(dirs, data)
	}
	keyList = cfg.GetKeyList("ReceiveUrls")
	for _, value := range keyList {
		receiveUrls = append(receiveUrls, cfg.MustValue("ReceiveUrls", value))
	}
	keyList = cfg.GetKeyList("ReportUrls")
	for _, value := range keyList {
		reportUrls = append(reportUrls, cfg.MustValue("ReportUrls", value))
	}

	//dfs
	appId = cfg.MustValue("DfsInfo", "appId")
	appSecret = cfg.MustValue("DfsInfo", "appSecret")
	hostUpload = cfg.MustValue("DfsInfo", "hostUpload")
	hostUpdate = cfg.MustValue("DfsInfo", "hostUpdate")
	hostCheck = cfg.MustValue("DfsInfo", "hostCheck")
	hostCheckUrl = cfg.MustValue("DfsInfo", "hostCheckUrl")
	promethusPort = cfg.MustInt("Promethus", "PromethusPort")

	//mysql
	mysqlInfo.IP = cfg.MustValue("MysqlWriterInfo", "ip")
	mysqlInfo.PORT, _ = strconv.Atoi(cfg.MustValue("MysqlWriterInfo", "port"))
	mysqlInfo.USERNAME = cfg.MustValue("MysqlWriterInfo", "username")
	mysqlInfo.PASSWORD = cfg.MustValue("MysqlWriterInfo", "password")
	mysqlInfo.DBNAME = cfg.MustValue("MysqlWriterInfo", "dbname")
	return nil
}

func main() {
	defer logger.Flush()
	if err := initialize(); err != nil {
		return
	}
	defer func() {
		if err := recover(); err != nil {
			fmt.Println("test 发生错误", err)
		}
	}()
	newServer := NewServer()
	//http调用接收
	newServer.httpVideoServer.SetDFS(appId, appSecret, hostUpload, hostUpdate, hostCheck, hostCheckUrl)
	newServer.httpVideoServer.SetMysqlInfo(mysqlInfo)
	go newServer.httpVideoServer.HttpVideoService.TaskConsumer()
	newServer.httpVideoServer.Init()
	//thrift调用接收
	//newServer.thriftVideServer.Init()
	//newServer.thriftVideServer.ThriftVideoService.FlamingoDFS.Init(appId, appSecret, hostUpload, hostUpdate, hostCheck, hostCheckUrl)
	//newServer.thriftVideServer.SetAddr(addr)
	//newServer.thriftVideServer.SetDirs(dirs)
	//newServer.thriftVideServer.SetReceiveUrls(receiveUrls)
	//newServer.thriftVideServer.SetReportUrls(reportUrls)
	//newServer.thriftVideServer.SetKey(key)
	//newServer.thriftVideServer.SetStage(stage)
	//newServer.thriftVideServer.SetServerType(serverType)
	//newServer.thriftVideServer.SetIdc(idc)
	//newServer.thriftVideServer.SetOptIdcs(optIdcs)
	//newServer.thriftVideServer.InitPrometheus(serverType, promethusPort)
	//newServer.thriftVideServer.Run()
	//newServer.httpVideoServer.Init()
}
